package com.school.base.action.admin;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.model.Subject;
import com.school.base.service.SubjectService;
import com.school.common.base.BaseAction;
import com.school.common.constant.Constant;
import com.school.common.util.SpringUtils;

public class SubjectAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 550617826372877490L;
	private static Logger logger = Logger.getLogger(SubjectAction.class);
	private static SubjectService service = (SubjectService) SpringUtils.getBean("base_subjectService");
	private Subject subject;
	private List<Subject> subjectList;

	public Subject getSubject() {
		return subject;
	}

	public void setSubject(Subject subject) {
		this.subject = subject;
	}

	public List<Subject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}

	public String list() {
		subjectList = service.getSubjectList(subject, pageBean);
		return SUCCESS;
	}

	public String edit() {
		if (subject != null && subject.getId() > 0) {
			subject = service.getSubjectById(subject.getId());
		}
		return SUCCESS;
	}

	public String editDo() {
		if ("POST".equals(this.getHttpServletRequest().getMethod())) {
			if (subject != null) {
				if (subject.getId() > 0) {
					try {
						service.updateSubject(subject);
						logger.info("Upadte subject.");
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				} else {
					try {
						service.insertSubject(subject);
						logger.info("Insert subject.");
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				}
			} else {
				logger.info("Not save subject:subject object is null.");
			}
		} else {
			logger.info("Not save subject:the request method is not 'post'.");
		}
		return SUCCESS;
	}

	public String delete() {
		if (subject != null && subject.getId() > 0) {
			try {
				service.deleteSubject(subject.getId());
			} catch (Exception e) {
				this.getHttpSession().setAttribute(Constant.ERROR_MESSAGE, e.getMessage());
			}
			logger.info("Delete subject");
		} else {
			logger.info("Delete subject fail:the subject's id is null.");
		}
		return SUCCESS;
	}
}
